package TuiLayer;
import ControlLayer.ProductCtr;
import ModelLayer.Product;
import java.util.Scanner;

/**
 * Vestbjerg project
 * @GroupB2 (UCN - Computer Science DMAJ0914) 
 */
public class ProductUI
{
    // instance variables
    private ProductCtr productCtr;
    private Product foundProduct;

    /**
     * Constructor for objects of class ProductUI
     */
    public ProductUI()
    {
        productCtr = new ProductCtr();
    }

    public void createProduct(String barcode, String name, String description, double price, int parLvl, int maxAmt,
    int discount, int stockAmt, String location) 
    {
        productCtr.createProduct(barcode, name, description, price, parLvl, maxAmt, discount, stockAmt, location);
    }

    public Product findProduct(String barcode) 
    {
        return productCtr.findProduct(barcode);
    }

    public void updateProduct(Product product, String barcode, String name, String description,double price, int parLvl, int maxAmt, int discount, int stockAmt) 
    {
        productCtr.updateProduct(product,barcode, name, description, price, parLvl, maxAmt, discount,
            stockAmt);
    }

    public void deleteProduct(Product product) 
    {
        productCtr.deleteProduct(product);
    }

    private int getChoice()
    {
        Scanner keyboard = new Scanner(System.in);
        int choice = keyboard.nextInt();

        return choice;
    }

    // Products menu
    public void menuStart() 
    {
        boolean exit = false;
        String print = null;
        while(!exit) 
        {
            writeProductsMenu(print);
            int choice = getChoice();

            if(choice == 1) 
            {
                print = productAddStart();
            }
            else if(choice == 2)
            {
                print = browseMenuStart();
            }
            else if(choice == 0) 
            {
                exit = true;
            }

        }
    }

    public String browseMenuStart() 
    {
        String print = null;

        boolean exit = false;
        print = productFindStart();
        if(foundProduct == null) 
        {
            exit = true;
        }
        while(!exit) 
        {
            writeBrowseProductMenu(print);
            int choice = getChoice();

            if(choice == 1) 
            {
                print = productUpdateStart();
            }
            else if(choice == 2)
            {
                print = productRemoveStart();

            }
            else if(choice == 0) 
            {
                print = null;
                exit = true;
            }

        }
        return print;
    }


    // Write products menu
    public void writeProductsMenu(String print) 
    {
        System.out.println("\f *** Products ***");
        System.out.println(" (1)Add");
        System.out.println(" (2)Find / Edit");
        System.out.println(" (0)Back");
        if(print != null) 
        {
            System.out.println("\n " + print);

        }
        System.out.print("\n Make your choice: ");
    }

    public void writeBrowseProductMenu(String print) 
    {
        System.out.println("\f *** Product barcode: " + foundProduct.getBarcode() + " ***");
        System.out.println(" (1)Edit");
        System.out.println(" (2)Delete");
        System.out.println(" (0)Back");
        if(print != null) 
        {
            System.out.println("\n " + print);

        }
        System.out.print("\n Make your choice: ");
    }

    // Start "Add a product"
    public String productAddStart() 
    {
        String print = null;
        
        System.out.println("\nEnter barcode: ");
        Scanner sc = new Scanner(System.in);
        String barcode = sc.nextLine();

        System.out.println("Enter name: ");
        String name = sc.nextLine();

        System.out.println("Enter description: ");
        String description = sc.nextLine();

        System.out.println("Enter price: ");
        double price = sc.nextDouble();

        System.out.println("Enter parLvl: ");
        int parLvl = sc.nextInt();

        System.out.println("Enter maximum amount: ");
        int maxAmt = sc.nextInt();

        System.out.println("Enter discount: ");
        int discount = sc.nextInt();

        System.out.println("Enter stock amount: ");
        int stockAmt = sc.nextInt();
        
        System.out.println("Enter location: ");
        String location = sc.next();
        
        
        Product product = productCtr.findProduct(barcode);
        if(product == null) 
        {
            createProduct(barcode, name, description, price, parLvl, maxAmt, discount, stockAmt, location);

            print = "Product #" + barcode + " has been added to your products.";
        }
        else 
        {
            print = "ERROR: product already exists.";
        }

        return print;
    }

    // Start "Find a product"
    public String productFindStart() 
    {
        String print = null;
        System.out.print("\nEnter barcode: ");

        Scanner sc = new Scanner(System.in);
        String barcode = sc.next();

        foundProduct = findProduct(barcode);
        if(foundProduct != null) 
        {
            print = " # Found Product: #" + "\n" 
            + "  Barcode: " + foundProduct.getBarcode() + "\n"
            + "  Name: " + foundProduct.getName() + "\n"
            + "  Description: " + foundProduct.getDescription()+ "\n"
            + "  Price: " + foundProduct.getPrice() + "\n"
            + "  Par level: " + foundProduct.getParLvl() + "\n"
            + "  Maximum stock: " + foundProduct.getMaxAmt() + "\n"
            + "  Discount: " + foundProduct.getDiscount() + "\n"
            + "  Stock: " + foundProduct.getStockAmt() + "\n"
            + "  Location: " + foundProduct.getLocation();           
        }
        else 
        {
            print = "ERROR: Product not found.";
        }
        return print;
    }

    // Start "Update a product"
    public String productUpdateStart() 
    {
        String print = null;
        System.out.println("\nEnter new barcode: ");
        Scanner sc = new Scanner(System.in);
        String newBarcode = sc.nextLine();

        System.out.println("Enter new name: ");
        String newName = sc.nextLine();

        System.out.println("Enter new description: ");
        String newDescription = sc.nextLine();

        System.out.println("Enter new price: ");
        double newPrice = sc.nextDouble();

        System.out.println("Enter new parLvl: ");
        int newParLvl = sc.nextInt();

        System.out.println("Enter new maximum amount: ");
        int newMaxAmt = sc.nextInt();

        System.out.println("Enter new discount: ");
        int newDiscount = sc.nextInt();

        System.out.println("Enter new stock amount: ");
        int newStockAmt = sc.nextInt();

        updateProduct(foundProduct, newBarcode, newName, newDescription, newPrice, newParLvl, newMaxAmt, newDiscount, newStockAmt);

        print = "Product #" + newBarcode + " has been updated to:\n" + getProductInfo(foundProduct);

        return print;
    }

    private String getProductInfo(Product product) 
    {
        String string = "  Barcode: " + product.getBarcode() + "\n"
            + "  Name: " + product.getName() + "\n"
            + "  Description: " + product.getDescription() + "\n"
            + "  Price: " + product.getPrice() + "\n"
            + "  parLvl: " + product.getParLvl() + "\n"
            + "  maxAmt: " + product.getMaxAmt() + "\n"
            + "  discount: " + product.getDiscount() + "\n"
            + "  stockAmt: " + product.getStockAmt() + "\n";
        return string;
    }

    // Start "Remove a product"
    public String productRemoveStart() 
    {
        String print = null;

        deleteProduct(foundProduct);
        print = "Product #" + foundProduct.getBarcode() + " has been deleted.";

        return print;
    }
}
